﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;

namespace WebServiceKFC.Utils
{
    public class UtilsXML
    {
        public static XmlDocument PortExAttToXML(List<string> DsMaMonAnDon, List<int> DsSoLuongMon)
        {
            StringBuilder sb = new StringBuilder();
            XmlWriter writer = XmlWriter.Create(sb);

            writer.WriteStartDocument();
            writer.WriteStartElement("ExtraAttributes");
            for (int i = 0; i < DsMaMonAnDon.Count; i++)
            {
                writer.WriteStartElement("MonAnDon");

                writer.WriteStartElement("MaMonAn");
                writer.WriteValue(DsMaMonAnDon[i]);
                writer.WriteEndElement();
                writer.WriteStartElement("SoLuong");
                writer.WriteValue(DsSoLuongMon[i]);
                writer.WriteEndElement();

                writer.WriteEndElement();
            }

            writer.WriteEndElement();
            writer.WriteEndDocument();

            writer.Flush();

            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(sb.ToString());
            return xmlDocument;
        }

        public static XmlDocument PortExAttToXML()
        {
            StringBuilder sb = new StringBuilder();
            XmlWriter writer = XmlWriter.Create(sb);

            writer.WriteStartDocument();
            writer.WriteStartElement("ExtraAttributes");
            writer.WriteEndElement();
            writer.WriteEndDocument();

            writer.Flush();

            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(sb.ToString());
            return xmlDocument;
        }

        public static void readXML(List<string> dsMaMonAnDon, List<int> dsSoLuong, XmlNode xmlDoc)
        {
            XmlNodeList list = xmlDoc.ChildNodes;
            foreach (XmlNode monAnDon in list)
            {
                foreach (XmlNode item in monAnDon)
                {
                    if (item.Name == "MaMonAn")
                        dsMaMonAnDon.Add(item.InnerText);
                    if (item.Name == "SoLuong")
                        dsSoLuong.Add(int.Parse(item.InnerText));
                }
            }
        }
    }
}